package com.heima.wemedia.controller.v1;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.api.wemeida.WmUserControllerApi;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.wemedia.service.WmUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/v1/user")
public class WmUserController implements WmUserControllerApi {
    @Autowired
    private WmUserService wmUserService;
    @PostMapping("/save")
    @Override //保存自媒体信息
    public ResponseResult save(@RequestBody WmUser wmUser) {
        if (wmUserService.save(wmUser)){
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
    }
    @GetMapping("/findByName/{name}")
    @Override//根据名称查询自媒体信息
    public WmUser findByname(@PathVariable("name") String name) {
        List<WmUser> list = wmUserService.list(new LambdaQueryWrapper<WmUser>().eq(WmUser::getName, name));
        if(!list.isEmpty()&&list!=null){//isEmpty是判断list中是否有内容，list！=null是为了不报空指针异常
            return list.get(0);
        }
        return null;
    }
    @GetMapping("/findOne/{id}")//根据id查询自媒体用户
    @Override
    public WmUser findWmUserById(@PathVariable("id") Long id) {
        return null;
    }
}
